#include "SplitTool.hh"
#include "SplitToolCppJieba.hh"
#include "cppjieba/Jieba.hpp"
#include "utils.hh"

SplitToolCppJieba::SplitToolCppJieba()
{
}
SplitToolCppJieba::~SplitToolCppJieba()
{
}
vector<string> SplitToolCppJieba::cut(const string &filePath)
{
    const char *const DICT_PATH = "/home/tk/course/search-engines/include/dict/jieba.dict.utf8";
    const char *const HMM_PATH = "/home/tk/course/search-engines/include/dict/hmm_model.utf8";
    const char *const USER_DICT_PATH = "/home/tk/course/search-engines/include/dict/user.dict.utf8";
    const char *const IDF_PATH = "/home/tk/course/search-engines/include/dict/idf.utf8";
    const char *const STOP_WORD_PATH = "/home/tk/course/search-engines/include/dict/stop_words.utf8";
    cppjieba::Jieba jieba(DICT_PATH, HMM_PATH, USER_DICT_PATH, IDF_PATH, STOP_WORD_PATH);

    vector<string> words;

    ifstream ifs(filePath);
    if (!ifs)
    {
        cerr << "ifs(filePath) if failed...\n";
        return words;
    }
    // 1.清洗，去掉\r \n 空格 【 】
    set<string> delSet({"【", "】","（","）","：","＊","，","。","、","《","》","；","”","—"});

    string line;
    string articleStr = "";

    while (getline(ifs, line))
    {
        string newLine = "";
        for (size_t i = 0; i < line.size();)
        {
            int num = Utils::nBytesCode(line[i]);
            if (num == 1)
            {
                // 英文
                i += num;
                continue;
            }
            else
            {
                string chineseCh = line.substr(i, num);
                // 中文
                if (delSet.count(chineseCh) != 0)
                {
                    i += num;
                    continue;
                }
                newLine += chineseCh;
                i += num;
            }
        }
        // 将整篇文章拼接为一个超常字符串
        articleStr += newLine;

        // jieba分词
        jieba.Cut(articleStr, words);
    }
    ifs.close();
    return words;
}

#if 0
int main(int argc, char const *argv[])
{
    SplitTool *st = new SplitToolCppJieba();
    vector<string> v = st->cut("/home/tk/review/project/data/yuliao/C3-Art0019.txt");
    for (auto &word : v)
    {
        cout << word << "\n";
    }
    cout << v.size() << "\n";
    return 0;
}
#endif
// g++ -o SplitToolCppJieba SplitToolCppJieba.cpp SplitTool.cpp ../utils/utils.cpp -I ../include/ -I ../utils/